<?php

/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * http://www.gnu.org/copyleft/gpl.html
 */
 


/**
 *  @file
 *  @Author Samuel Maura
 *  Create a group
 */

require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/controllers/debug.inc");
require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/controllers/validate.inc");
require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/lib/session/sessionUtils.inc");

// Create session
session_start();

//Copy post values

// save original values in session
savePost();

$_SESSION['validation_error'] = "";

$validatedInputs = array();
$inputs = trimInputArray($_POST);
$inputs = removeHtmlTags($inputs);

validateGroupEntry($inputs['group_label'],$inputs['description'],$inputs['access_type_id']);
checkErrors('validation_error','/xenix/views/wikicreategroup.php');



// Check again
 if (($_SESSION['validation_error'] == '') && (isset($_SESSION['user_id'])) && ($_SESSION['user_id'] != '') ){
    require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/models/groups.php");
    require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/models/sqlSafe.php");
    
    $group = new Groups();
    $sqlSafe = new SqlSafe(dbManager::connect());
    
    foreach($validatedInputs as $key => $val){
      $validatedInputs[''.$key.''] = $sqlSafe->safe($val);
    }
    
     $validatedInputs['id'] = mt_rand(100000,999999).mt_rand(100000,999999);
     $validatedInputs['created_by'] = $_SESSION['user_id'];
    
    if ($group->groupExist($validatedInputs['label'])){
        $_SESSION['validation_error'] = "This group name already exist.";
        header("Location: /xenix/views/wikicreategroup.php");
        exit;
    }
    
    $group->add($validatedInputs);
    $_SESSION['validation_error'] = "Group ".$validatedInputs['label']." added.";
    header("Location: /xenix/views/wikicreategroup.php");
 }
 else{
   header("Location: ../views/wikierror.php");
 }



function validateGroupEntry($label,$description,$access_type){
  if ( isset($label) && ($label != '')){
    
    if (preg_match('/^[a-z]+[a-z0-9]+/i',$label)){
        $label =  preg_replace('/[^a-z0-9_-]+/i','', $label);
        $GLOBALS['validatedInputs']['label'] = preg_replace('/[\s|\-]+/','_', $label);
        //echo $GLOBALS['validatedInputs']['label']."<br/>";
    }
    else{
       $_SESSION['validation_error'] = "Label format is invalid";
    }
  }
  else{
    $_SESSION['validation_error'] = "Label is required";
  }
  
  
  if ( isset($description) && ($description != '')){
    $GLOBALS['validatedInputs']['description'] = $description;
  }
  else{
    $_SESSION['validation_error'] = "description is required";
  }
  
  
  if ( isset($access_type) && ($access_type != '') ){
    if (is_numeric($access_type)){
      $GLOBALS['validatedInputs']['access_type_id'] = $access_type;
    }
    else{
      $_SESSION['validation_error'] = "access type format is invalid";
    }
  }
  else{
    $_SESSION['validation_error'] = "description is required";
  }
  
}


// Show debug info
//echo $GLOBALS['POST_INPUTS'];


?>